//上传-预览逻辑
    $(".newspic").click(function() {    //1.点击默认图片，触发标签<a>的click事件
        $(this).next('input').click();//2.触发input的点击事件，用户选择图片进行上传
    });    
    function showPic(data) {//3.当用户上传图片后，触发input标签的onchange事件，执行showPic()方法
        //获取上传文件的信息
        var pic =document.getElementById('pic').files[0];
        //在pic的文件里抓取该文件用于显示二进制信息
        var sr  = window.URL.createObjectURL(pic);
        //设置src属性，显示图片
        document.getElementsByTagName('img')[0].src=sr;
        document.getElementsByTagName('img')[0].onload = function() {
            window.URL.revokeObjectURL(this.src);
        }
    }

$(function($) {
	var entityID = getQueryString('id');
	if (entityID)
		$.post(contextPath + "/issuePeriod/getIssuePeriod", {"id": entityID}, function(result) {
			//alert(JSON.stringify(result));alert(result['issueEndTime']);alert(result.issueEndTime);
			result.issueStartTime = result.issueStartTime ? new Date(result.issueStartTime).pattern("yyyy-MM-dd HH:mm:ss") : '';
			result.issueEndTime = result.issueEndTime ? new Date(result.issueEndTime).pattern("yyyy-MM-dd HH:mm:ss") : '';
			result.validityStartTime = result.validityStartTime ? new Date(result.validityStartTime).pattern("yyyy-MM-dd HH:mm:ss") : '';
			result.validityEndTime = result.validityEndTime ? new Date(result.validityEndTime).pattern("yyyy-MM-dd HH:mm:ss") : '';
			$("#form-add").JsonToForm(result);
			$("#imageId").attr("src","/image/" + result.cardFacePic);
		});
	
	getSelectList();
	
	$("#form-add").validate({
		rules: {
			issuePeriodAlias:{
				required:true,
				maxlength:40,
			},
			issueStartTime:{
				required:true,
			},
			issueEndTime:{
				required:true,
			},
			salePrice:{
				required:true,
				isDigits:true,
			},
			validityStartTime:{
				required:true,
			},
			validityEndTime:{
				required:true,
			},
			saleRatio:{
				required:true,
				isDigits:true,
			},
			swipeTimes:{
				required:true,
				isDigits:true,
				isIntGtZero:true,
			},
			cardType:{
				required:true,
			}
		},
		onkeyup: function(element){
			$(element).valid();
		},
		submitHandler: function(form) {
			var formData = new FormData($("#form-add")[0]);  
			$.ajax({
				type: "post",
				url: contextPath + "/issuePeriod/" + (entityID ? "update" : "add"),
				data: formData,//$(form).serialize(),
				dataType: "json",
				cache: false,
				async:false,
	            processData: false,
	            contentType: false, 
				success: function(data) {
					if(data.status == 200) {
						var message = entityID ? '更新成功!' : '添加成功!';
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引,隐藏layer层和shade
						parent.$('#layui-layer' + index).css({'display': 'none'});
						parent.$('#layui-layer-shade' + index).css({'display': 'none'});
						parent.reloadTable(); //再刷新DT
						parent.showSuccessMessage(message, null, function() {
							parent.layer.close(index); //然后执行关闭     
						});
					} else {
						var message = entityID ? '添加成功!' : '添加失败!';
						parent.showFailMessage(message);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(errorThrown);
					return false;
				}
			});
			return false;
		}
	});

});
function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = location.search.substr(1).match(reg);
	if(r != null)
		return unescape(r[2]);
	return null;
}

function getSelectList(){
	jQuery.ajax({
	      type:"POST",
	      dataType:"json",
	      url:contextPath + "/cardType/getCardTypeSelectList",
	      success:function(data){
	        var unitObj=document.getElementById("selectList");
	        if(data != null){ 
	        	//alert(JSON.stringify(selectList));
	          for(var i = 0; i < data.length; i++){
	          //先创建好select里面的option元素
	            var option = document.createElement("option");
	            //转换DOM对象为JQ对象,好用JQ里面提供的方法 给option的value赋值
	            $(option).val(data[i].value);
	            //给option的text赋值,这就是你点开下拉框能够看到的东西
	            $(option).text(data[i].value + ":"+ data[i].text);
	            //获取select 下拉框对象,并将option添加进select
	            $('#selectList').append(option);
	            if (i == 0)
	            	option.selected = true;
	          }
	        }
	      },
	      error:function(){
	        J.alert('Error loading Json document');        
	      }
	    });
}